#include "GameObject.h"
#include "TextureManager.h"
#include "Game.h"
#include "InputHangdler.h"

GameObject::~GameObject() {}

void GameObject::load(int x, int y, int width, int height, int _radius,
	std::string textureID)
{
	box.x = m_position.x = x;
	box.y = m_position.y = y;
	box.w = m_width = width;
	box.h = m_height = height;

	square.x_center = (width + x) / 2;
	square.y_center = (height + y) / 2;
	square.radius = _radius;

	m_textureID = textureID;
}

void GameObject::add(Sprite &_sprite, int &_idSprite, SDL_Renderer* _pRenderer, Map* _map)
{
	m_sprite = _sprite;
	m_idSprite = _idSprite;
	m_pRenderer = _pRenderer;
	m_map = _map;
	m_position.x = m_sprite.getSpriteImage()[m_idSprite].image_possition_x;
	m_position.y = m_sprite.getSpriteImage()[m_idSprite].image_possition_y;
	TheTextureManager::Instance()->add(m_sprite, m_pRenderer);
}

void GameObject::draw()
{
	TheTextureManager::Instance()->drawParallax(m_sprite, m_idSprite, m_pRenderer);
}

void GameObject::update(Point &possition)
{
	//
}

void GameObject::clean()
{
	//
}

/////////////////////////////////////////////////////////////////////////

Point & GameObject::getPosition()
{
	return m_position;
}

int GameObject::getWidth() const
{
	return m_width;
}

int GameObject::getHeight() const
{
	return m_height;
}

float GameObject::getRadius() const
{
	return m_radius;
}

void GameObject::setIsExpired(const int &status)
{
	if (status == 1) m_IsExpired = true;
	if (status == 0) m_IsExpired = false;
}

bool GameObject::isExpired()
{
	// error
	return true;
	//return m_IsExpired;
}

Vector2D & GameObject::setVelocity(const Vector2D& _velocity)
{
	m_velocity = _velocity;
	return m_velocity;
}

Vector2D & GameObject::setAccelertion(const Vector2D& _accelertion)
{
	m_acceleration = _accelertion;
	return m_acceleration;
}

SDL_Rect GameObject::GetBoundingBoxObject(std::string id)
{
	Uint32 format;
	int access;
	int w;
	int h;
	
	map<string, SDL_Texture*> textureMap;
	SDL_QueryTexture(textureMap[id], &format, &access, &w, &h);
	SDL_Rect bb = { m_position.x, m_position.y, w, h };
	return bb;
}

void GameObject::setSpeed(const float &speed)
{
	m_speed = speed;
}

SDL_Rect GameObject::getBox() const
{
	return box;
}

cSquare GameObject::getSquare() const
{
	return square;
}

//std::vector<SDL_Rect> & GameObject::get_rect() const
//{
//	//return boxPixel;
//}

int GameObject::CheckLeft(int &_x, int &_y)
{
	int a = 0;
	for (int i = _x; i < _x+ 20; i++)
	{
		for (int j = _y; j < _y + 20; j++)
		{
			int _i = i - 1;
			int statusTitle = m_map->findIdTitles(_i, j);
			if (m_map->getLocationTitles()[statusTitle] == 1)
			{
				a = 1;
			}
		}
	}
	return a;
	
}
int GameObject::CheckRight(int &_x, int &_y)
{
	for (int i = _x; i < _x + 20; i++)
	{
		for (int j = _y; j < _y + 20; j++)
		{
			int _i = i + 1;
			int statusTitle = m_map->findIdTitles(_i, j);
			if (m_map->getLocationTitles()[statusTitle] == 1)
			{
				return 1;
			}
		}
	}
	return 0;
}
int GameObject::CheckUp(int &_x, int &_y)
{
	for (int i = _x; i < _x + 20; i++)
	{
		for (int j = _y; j < _y + 20; j++)
		{
			int _j = j - 1;
			int statusTitle = m_map->findIdTitles(i, _j);
			if (m_map->getLocationTitles()[statusTitle] == 1)
			{
				return 1;
			}
		}
	}
	return 0;
}
int GameObject::CheckDown(int &_x, int &_y)
{

	for (int i = _x ; i < _x + 20; i++)
	{
		for (int j = _y; j < _y + 20; j++)
		{
			int _j = j + 1;
			int statusTitle = m_map->findIdTitles(i, _j);
			if (m_map->getLocationTitles()[statusTitle] == 1)
			{
				return 1;
			}
		}
	}
	return 0;
}